//
//  SEGActivityLotteryBreakEggsView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/16.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityLotteryBreakEggsView.h"

@interface SEGActivityLotteryBreakEggsView()
@property (nonatomic, copy)GoldenEggsBlock block;
@end

@implementation SEGActivityLotteryBreakEggsView
{
    UIImageView *eggMiddle;
    UIImageView *eggLeft;
    UIImageView *eggRight;
    UIImageView *hammer;
    NSMutableArray*imgeBigs;
    NSMutableArray*imgeSmalls;
    UIImageView *backgorundView;
    
}
-(instancetype)initWithFrame:(CGRect)frame finished:(GoldenEggsBlock)block{
    if (self == [super initWithFrame:frame]) {
        _block = block;
        [self setUserInteractionEnabled:YES];
        [self setupUI];
    }
    return self;
}

-(void)seg_middleClick{

    if (self.block) {
        self.block(1);
    }
}

-(void)seg_leftClick{

    if (self.block) {
        self.block(0);
    }
}

-(void)seg_rightClick{

    if (self.block) {
        self.block(2);
    }
}

-(void)setupUI{
    //背景
    backgorundView = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_bg")]];
    [self addSubview:backgorundView];
    
    
    //中间的金蛋
    eggMiddle = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_egg01")]];
    [self addSubview:eggMiddle];
    UITapGestureRecognizer *tapMiddle = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seg_middleClick)];
    [eggMiddle addGestureRecognizer:tapMiddle];
    [eggMiddle setUserInteractionEnabled:YES];
    
    
    //左边的金蛋
    eggLeft = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_smallegg01")]];
    [self addSubview:eggLeft];
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seg_leftClick)];
    [eggLeft addGestureRecognizer:tapLeft];
    [eggLeft setUserInteractionEnabled:YES];
    
    
    //右边的金蛋
    eggRight = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_smallegg01")]];
    [self addSubview:eggRight];
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(seg_rightClick)];
    [eggRight addGestureRecognizer:tapRight];
    [eggRight setUserInteractionEnabled:YES];
    
    
    //锤子
    hammer = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_activity03_chuizi")]];
    [self addSubview:hammer];
  
}

-(void)seg_imgeBigs_get{
    //大图的砸金蛋动画
    imgeBigs = [NSMutableArray array];
    for (NSInteger i = 1; i <=5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"img_activity03_egg0%zd",i];
        [imgeBigs addObject:[UIImage imageWithContentsOfFile:SEGPicName(imageName)]];
    }
}

-(void)seg_imgeBigs_miss{
    imgeBigs = [NSMutableArray array];
    for (NSInteger i = 1; i <=6; i++) {
        if (i != 5) {
            NSString *imageName = [NSString stringWithFormat:@"img_activity03_egg0%zd",i];
            [imgeBigs addObject:[UIImage imageWithContentsOfFile:SEGPicName(imageName)]];
        }
    }
}

-(void)seg_imgeSmalls_get{
    //小图的砸金蛋动画
    imgeSmalls = [NSMutableArray array];
    for (NSInteger i = 1; i <=5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"img_activity03_smallegg0%zd",i];
        [imgeSmalls addObject:[UIImage imageWithContentsOfFile:SEGPicName(imageName)]];
    }
}

-(void)seg_imgeSmalls_miss{
    imgeSmalls = [NSMutableArray array];
    for (NSInteger i = 1; i <=6; i++) {
        if (i != 5) {
            NSString *imageName = [NSString stringWithFormat:@"img_activity03_smallegg0%zd",i];
            [imgeSmalls addObject:[UIImage imageWithContentsOfFile:SEGPicName(imageName)]];
        }
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [backgorundView setFrame:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    
    CGFloat eggMiddle_W = LYScreenScaleWidth() * 190;
    CGFloat eggMiddle_H = (eggMiddle_W *18.0)/ 19.0;
    CGFloat eggMiddle_X = (CGRectGetWidth(self.frame) - eggMiddle_W)*0.5;
    CGFloat eggMiddle_Y = (CGRectGetHeight(self.frame) * 140)/375.0;
    [eggMiddle setFrame:CGRectMake(eggMiddle_X, eggMiddle_Y, eggMiddle_W, eggMiddle_H)];
    
    CGFloat eggLeft_W = LYScreenScaleWidth() * 140;
    CGFloat eggLeft_H = (eggMiddle_W *18.0)/ 19.0;
    CGFloat eggLeft_X = 0.0;
    CGFloat eggLeft_Y = eggMiddle_Y;
    [eggLeft setFrame:CGRectMake(eggLeft_X, eggLeft_Y, eggLeft_W, eggLeft_H)];
    
    CGFloat eggRight_W = LYScreenScaleWidth() * 140;
    CGFloat eggRight_H = (eggMiddle_W *18.0)/ 19.0;
    CGFloat eggRight_X = CGRectGetWidth(self.frame) - eggRight_W;
    CGFloat eggRight_Y = eggMiddle_Y;
    [eggRight setFrame:CGRectMake(eggRight_X, eggRight_Y, eggRight_W, eggRight_H)];
    
    
    CGFloat hammer_W = LYScreenScaleWidth() * 120;
    CGFloat hammer_H = hammer_W/ 2.4;
    CGFloat hammer_X = (CGRectGetWidth(self.frame) - hammer_W) * 0.5;
    CGFloat hammer_Y = (CGRectGetHeight(self.frame) * 110)/375.0;
    [hammer setFrame:CGRectMake(hammer_X, hammer_Y, hammer_W, hammer_H)];
}


/// 锤子移动动画
- (void)startHummerMoveAnimation {
    [hammer.layer removeAllAnimations];
    hammer.hidden = NO;
    
    
    CAKeyframeAnimation *rectRunAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    //设定关键帧位置，必须含起始与终止

    rectRunAnimation.values = @[@(0),
                                @(LYScreenWidth()/2.0 - LYScreenScaleWidth() * 70),
                                @(-LYScreenWidth()/2.0 + LYScreenScaleWidth() * 70),
                                @(0)];
       
    rectRunAnimation.calculationMode = kCAAnimationDiscrete;
    rectRunAnimation.repeatCount = 1000;
    rectRunAnimation.autoreverses = NO;
    rectRunAnimation.duration = 4;
    [hammer.layer addAnimation:rectRunAnimation forKey:@"rectRunAnimation"];
}

-(void)startAnimatin:(BOOL)isGetWallet eggIndex:(NSInteger)index{
    
    if (index == 0) {
        if (isGetWallet) {
            [self seg_imgeSmalls_get];
        }else{
            [self seg_imgeSmalls_miss];
        }
        [hammer setHidden:YES];
        eggLeft.animationImages = imgeSmalls;
        eggLeft.animationDuration = 1.5;
        eggLeft.animationRepeatCount = 1;
        [eggLeft startAnimating];
    }
    
    
    if (index == 1) {
        [hammer setHidden:YES];
        if (isGetWallet) {
            [self seg_imgeBigs_get];
        }else{
            [self seg_imgeBigs_miss];
        }
        eggMiddle.animationImages = imgeBigs;
        eggMiddle.animationDuration = 1.5;
        eggMiddle.animationRepeatCount = 1;
        [eggMiddle startAnimating];
    }
    
    if (index == 2) {
        [hammer setHidden:YES];
        if (isGetWallet) {
            [self seg_imgeSmalls_get];
        }else{
            [self seg_imgeSmalls_miss];
        }
        eggRight.animationImages = imgeSmalls;
        eggRight.animationDuration = 1.5;
        eggRight.animationRepeatCount = 1;
        [eggRight startAnimating];
    }
    
}
@end

